import Util from './js/util.js'
import deepseek from './js/llm/deepseek.js'

//这个函数在整个wps加载项中是第一个执行的
function OnAddinLoad(ribbonUI) {
  return true
}

function callbackFunction(respMsg, range) {
  return range.InsertAfter(respMsg)
}
function OnAction(control) {
  const eleId = control.Id
  switch (eleId) {
    case 'contentReWrite':
      {
        let tsPolishId = window.Application.PluginStorage.getItem('llm_polish_taskpane_id')
        if (tsPolishId) {
          let tskpane = window.Application.GetTaskPane(tsPolishId)
          tskpane.Visible = false
        }
        let tsId = window.Application.PluginStorage.getItem('llm_write_taskpane_id')
        if (!tsId) {
          let tskpane = window.Application.CreateTaskPane(Util.GetUrlPath() + Util.GetRouterHash() + '/continuedwriting')
          let id = tskpane.ID
          window.Application.PluginStorage.setItem('llm_write_taskpane_id', id)
          tskpane.Visible = true
        } else {
          let tskpane = window.Application.GetTaskPane(tsId)
          tskpane.Visible = !tskpane.Visible
        }
      }
      break
    case 'resetMsg':
      deepseek.clearMsg()
      break
    case 'llmConfig': {
      window.Application.ShowDialog(
        Util.GetUrlPath() + Util.GetRouterHash() + '/llmConfigTp',
        '大模型配置',
        450 * window.devicePixelRatio,
        500 * window.devicePixelRatio,
        true
      )
    }
      break
    case 'polish': {
      let tsId = window.Application.PluginStorage.getItem('llm_polish_taskpane_id')
      let tsWriteId = window.Application.PluginStorage.getItem('llm_write_taskpane_id')
      if (tsWriteId) {
        let tskpane = window.Application.GetTaskPane(tsWriteId)
        tskpane.Visible = false
      }
      if (!tsId) {
        let tskpane = window.Application.CreateTaskPane(Util.GetUrlPath() + Util.GetRouterHash() + '/polish')
        let id = tskpane.ID
        window.Application.PluginStorage.setItem('llm_polish_taskpane_id', id)
        tskpane.Visible = true
      } else {
        let tskpane = window.Application.GetTaskPane(tsId)
        tskpane.Visible = !tskpane.Visible
      }
    }
      break
    case 'btnShowTaskPane':
      {
        let tsId = window.Application.PluginStorage.getItem('taskpane_id')
        if (!tsId) {
          let tskpane = window.Application.CreateTaskPane(Util.GetUrlPath() + Util.GetRouterHash() + '/taskpane')
          let id = tskpane.ID
          window.Application.PluginStorage.setItem('taskpane_id', id)
          tskpane.Visible = true
        } else {
          let tskpane = window.Application.GetTaskPane(tsId)
          tskpane.Visible = !tskpane.Visible
        }
      }
      break
    default:
      break
  }
  return true
}

function GetImage(control) {
  const eleId = control.Id
  switch (eleId) {
    case 'llmConfig':
      return 'images/config.svg'
    case 'polish':
      return 'images/polish.svg'
    case 'contentReWrite':
      return 'images/full.svg'
    default:
  }
  return 'images/newFromTemp.svg'
}

function OnGetEnabled(control) {
  const eleId = control.Id
  switch (eleId) {
    case 'btnShowMsg':
      return true
    case 'btnShowDialog': {
      let bFlag = window.Application.PluginStorage.getItem('EnableFlag')
      return bFlag
    }
    case 'btnShowTaskPane': {
      let bFlag = window.Application.PluginStorage.getItem('EnableFlag')
      return bFlag
    }
    default:
      break
  }
  return true
}

function OnGetVisible(control) {
  const eleId = control.Id
  console.log(eleId)
  return true
}

function OnGetLabel(control) {
  const eleId = control.Id
  switch (eleId) {
    case 'btnIsEnbable': {
      let bFlag = window.Application.PluginStorage.getItem('EnableFlag')
      return bFlag ? '按钮Disable' : '按钮Enable'
    }
    case 'btnApiEvent': {
      let bFlag = window.Application.PluginStorage.getItem('ApiEventFlag')
      return bFlag ? '清除新建文件事件' : '注册新建文件事件'
    }
  }
  return ''
}

function OnNewDocumentApiEvent(doc) {
  alert('新建文件事件响应，取文件名: ' + doc.Name)
}

//这些函数是给wps客户端调用的
export default {
  OnAddinLoad,
  OnAction,
  GetImage,
  OnGetEnabled,
  OnGetVisible,
  OnGetLabel,
  OnNewDocumentApiEvent
}
